package activities;

import bookless.pakage.BookHandler;
import bookless.pakage.R;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AddBookActivity extends Activity{

	private static Context context;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.addbook);
		
		context = AddBookActivity.this;
		
		Button addBook;
		addBook = (Button) findViewById(R.id.submitBook);
		addBook.setOnClickListener(new View.OnClickListener() {
	        
        	@Override
        	public void onClick(View v) {
        		
        		EditText t = (EditText) findViewById(R.id.bookTitle);
        		String title = t.getText().toString();
        		EditText a = (EditText) findViewById(R.id.author);
        		String author = a.getText().toString();
        		EditText p = (EditText) findViewById(R.id.price);
        		String sPrice = (p.getText().toString());
        		EditText c = (EditText) findViewById(R.id.comment);
        		String comment = c.getText().toString();

        		String response = BookHandler.addBook(title, author, sPrice, comment);
        		
        		//messages depending if the book was added or not
        		if(response.equals("ok")){
        			Dialog d = new Dialog(AddBookActivity.this);
					d.setTitle("Success!");
					TextView tv = new TextView(AddBookActivity.this);
					tv.setText("Your book was succsessfully added, you can now enter a new book");
					d.setContentView(tv);
					d.show();     			
        		}else if(response.equals("noInPut")){
        			Dialog d = new Dialog(AddBookActivity.this);
					d.setTitle("Error!");
					TextView tv = new TextView(AddBookActivity.this);
					tv.setText("You must fill in all text fields");
					d.setContentView(tv);
					d.show();
        		}else if(response.equals("didntWork")){
        			Dialog d = new Dialog(AddBookActivity.this);
					d.setTitle("Error!");
					TextView tv = new TextView(AddBookActivity.this);
					tv.setText("The system couldn't add your book");
					d.setContentView(tv);
					d.show();
        		}
        	}
        });
		
		Button back;
		back = (Button) findViewById(R.id.backMyPage);
		back.setOnClickListener(new View.OnClickListener() {
	        
        	@Override
        	public void onClick(View v) {
        		startActivity(new Intent("bookless.pakage.MYPAGE"));	
        	}
        });
		
	}
	
	public static Context getContext(){
		return context;
	}
	

}
